//
//  JZChargeRecordVC.m
//  JFTwxsd
//
//  Created by Peter on 2018/4/10.
//  Copyright © 2018年 junfeiweiyejiaoyujituan. All rights reserved.
//

#import "JZChargeRecordVC.h"
#import "JZChargeRecordCell.h"
#import "JZChargeResultDescpVC.h"
#import "JZChargeRecordHeaderView.h"

@interface JZChargeRecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewT;

///指示器
@property (weak, nonatomic) IBOutlet UIView *indicatorView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorW;
///用户头像图片
@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImgT;
///用户昵称
@property (weak, nonatomic) IBOutlet UILabel *nickName;
///累计充话费描述
@property (weak, nonatomic) IBOutlet UILabel *phoneChargeDescp;
///充话费
@property (weak, nonatomic) IBOutlet UILabel *phoneCharge;
///累计充流量描述
@property (weak, nonatomic) IBOutlet UILabel *chrgeFlowDescp;
///充流量
@property (weak, nonatomic) IBOutlet UILabel *chargeFlow;
///充值次数描述
@property (weak, nonatomic) IBOutlet UILabel *chargeCountDescp;
///充值次数
@property (weak, nonatomic) IBOutlet UILabel *chargeCount;

@property (strong, nonatomic) NSDictionary *dataDic;
///充话费数据源
@property (strong, nonatomic) NSArray *phoneArr;
///充流量数据源
@property (strong, nonatomic) NSArray *flowArr;

@end

@implementation JZChargeRecordVC

#pragma mark --------生命周期

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLayoutSubviews {
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNav];
    [self configTableView];
    [self configTopView];
    
    _dataDic = [NSDictionary dictionary];
    _phoneArr = [NSArray array];
    _flowArr = [NSArray array];
    
    [self requestListData];
}

#pragma mark --------界面

- (void)configTopView {
    
    _phoneCharge.text = Format(@"%.2f",[_dataDic[@"mobile"] floatValue] ?: 0.00);
    _chargeFlow.text = Format(@"%.2f",[_dataDic[@"flow"] floatValue] ?: 0.00);
    _chargeCount.text = Format(@"%d",[_dataDic[@"times"] intValue] ?: 0);
    if (CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(320, 568))) {
        _phoneChargeDescp.font = [UIFont systemFontOfSize:11];
        _chrgeFlowDescp.font = [UIFont systemFontOfSize:11];
        _chargeCountDescp.font = [UIFont systemFontOfSize:11];
    }
}

- (void)configTableView {
    self.mainTableView.estimatedRowHeight = 91;
    self.mainTableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)configNav {
    
}

#pragma mark --------tableView方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
//    UIView *view = [[NSBundle mainBundle] loadNibNamed:JZChargeRecordHeaderView.nibName owner:nil options:nil].lastObject;
//    return view;
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 24;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.0001;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_indicatorL.constant == 0) {
        return _phoneArr.count;
    }
    return _flowArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JZChargeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JZChargeRecordCell" forIndexPath:indexPath];
    cell.rootTarget = self;
    cell.selector = @selector(cellClick:button:);
    if (_indicatorL.constant == 0) {
        if (_phoneArr.count > 0) {
            [cell configCellContent:_phoneArr[indexPath.row]];
        }
    } else {
        if (_flowArr.count > 0) {
            [cell configCellContent:_flowArr[indexPath.row]];
        }
    }
    return cell;
}

- (void)cellClick:(JZChargeRecordCell *)cell button:(UIButton *)btn {
    NSIndexPath *idxPth = [self.mainTableView indexPathForCell:cell];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JZChargeSb" bundle:nil];
    JZChargeResultDescpVC *vc = [sb instantiateViewControllerWithIdentifier:@"JZChargeResultDescpVC"];
    if (_indicatorL.constant == 0) {
        vc.chargeResult = _phoneArr[idxPth.row][@"is_success"];
    } else {
        vc.chargeResult = _flowArr[idxPth.row][@"is_success"];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -------话费，流量
///999123话费，9991234流量
- (IBAction)tabSelectAction:(UIButton *)sender {
    if (sender.tag == 999123) {
        NSLog(@"话费");
         _indicatorL.constant = 0;
    } else if (sender.tag == 9991234) {
        NSLog(@"流量");

    }
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
    [self.mainTableView reloadData];
}

#pragma mark ---------网络请求

- (void)requestListData {
    __weak typeof(self) weakSelf = self;
    NSDictionary *paramDic = @{@"userId":@""};
    
    //  priority = NSURLSessionTaskPriorityHigh;
}

@end
